/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.sun.kvem.midp.pim.formats.EndMatcher;

public class KMLDecoder {
	private String xmlResponseString;
	private KXmlParser parser;
	
	private String code = null;
	
	public KMLDecoder() {
		parser = new KXmlParser();
	}
	
	public void parse() throws XmlPullParserException, IOException{
		ByteArrayInputStream s = new ByteArrayInputStream(xmlResponseString.getBytes());		

//		parser.setInput(s, "UTF-8");
//		parser.nextTag();
//		parser.require(XmlPullParser.START_TAG, null, "kml");
//		continueTillNode("code");
//		parser.require(XmlPullParser.START_TAG, null, "code");
//		
//		code = parser.nextText();
//		parser.require(XmlPullParser.END_TAG, null, "code");
//		parser.nextTag();
//		parser.require(XmlPullParser.START_TAG, null, "request");
//		parser.nextTag();
//		parser.require(XmlPullParser.END_TAG, null, "request");
//		parser.nextTag();
//		parser.require(XmlPullParser.END_TAG, null, "Status");
//		
//		//TODO if code != 200 exit here
//		parser.require(XmlPullParser.START_TAG, null, "Point");
//		parser.nextTag();
//		parser.require(XmlPullParser.START_TAG, null, "coordinates");
//		String coordinates = parser.nextText();
//		parser.require(XmlPullParser.END_TAG, null, "coordinates");
//		parser.nextTag();
//		parser.require(XmlPullParser.END_TAG, null, "Point");
//		parser.nextTag();
//		parser.require(XmlPullParser.END_TAG, null, "Placemark");
//		parser.nextTag();
//		parser.require(XmlPullParser.END_TAG, null, "Response");
//		parser.nextTag();
//		parser.require(XmlPullParser.END_TAG, null, "kml");
	}
	
	private void gotoStartTag(String nodeName) throws XmlPullParserException, IOException{
		while(parser.nextTag() == XmlPullParser.START_TAG){
			
			parser.nextTag();
		}
	}
	
	private void gotoEndTag(String nodeName) throws XmlPullParserException, IOException{
		while(parser.nextTag() == XmlPullParser.END_TAG){
			
		}
	}

	public void setXmlResponseString(String xmlResponseString) {
		this.xmlResponseString = xmlResponseString;
	}
	
}
